<template>
  <a-form
    ref="formRef"
    :model="formState"
    name="basic"
    :label-col="{ span: 6 }"
    :wrapper-col="{ span: 14 }"
    autocomplete="off"
    @finish="onFinished"
    @finishFailed="onFinishFailed"
  >
    <a-form-item label="用户名" name="username" :rules="[{ required: true, message: '请输入' }]">
      <a-input v-model:value="formState.username" />
    </a-form-item>

    <a-form-item label="密码" name="password" :rules="[{ required: true, message: '请输入' }]">
      <a-input-password v-model:value="formState.password" />
    </a-form-item>

    <a-form-item label="姓名" name="realname" :rules="[{ required: true, message: '请输入' }]">
      <a-input v-model:value="formState.realname" />
    </a-form-item>

    <a-form-item label="部门" name="dept">
      <a-input v-model:value="formState.dept" />
    </a-form-item>

    <a-form-item label="权限" name="role" :rules="[{ required: true, message: '请输入' }]">
      <a-select ref="select" :disabled="isEdit" placeholder="请选择" v-model:value="formState.role">
        <a-select-option v-for="item in roleOptions" :key="item.id" :value="item.id">{{
          item.name
        }}</a-select-option>
      </a-select>
    </a-form-item>

    <a-form-item
      style="margin-top: -16px"
      :wrapper-col="{ offset: 6, span: 16 }"
      name="perms"
      v-show="formState.role === 2"
    >
      <a-checkbox-group v-model:value="formState.perms" :options="plainOptions" />
    </a-form-item>

    <a-form-item label="备注" name="remark">
      <a-input v-model:value="formState.remark" />
    </a-form-item>

    <a-form-item :wrapper-col="{ offset: 6, span: 16 }">
      <a-button type="primary" html-type="submit">确定</a-button>
    </a-form-item>
  </a-form>
</template>
  <script lang="ts">
import { defineComponent, ref } from 'vue';
import { defHttp } from '/@/utils/http/axios';

interface FormState {
  username: string;
  password: string;
  realname: string;
  remark: string;
  dept: string;
  role: number | null;
  perms: [];
}
export default defineComponent({
  setup(props, { emit }) {
    const formRef = ref();
    const roleOptions = ref([]);
    let isEdit = ref(false);
    const plainOptions = ['项目集成', '设备管理', '预警管理', '健康分析', '报表管理', '系统管理'];

    let formState = ref<FormState>({
      username: '',
      password: '',
      realname: '',
      remark: '',
      dept: '',
      role: null,
      perms: [],
    });

    const onFinishFailed = (errorInfo: any) => {
      console.log('Failed:', errorInfo);
    };

    const setForm = (formData) => {
      if (formData) {
        isEdit.value = true;
        formState.value = {
          username: formData.username,
          password: '',
          realname: formData.name,
          remark: formData.comment,
          dept: '',
          role: formData.rId,
          perms: [],
        };
        // console.log(formData, formState.value);
      } else {
        isEdit.value = false;
        formRef.value.resetFields();
      }
      // console.log(formState.value.realname, 2222);
    };

    const onFinished = (event) => {
      emit('finished', { ...event });
    };

    function getRoleOptions() {
      defHttp.get({ url: '/role/get-all' }).then((res) => {
        roleOptions.value = res.data;
      });
    }
    getRoleOptions();

    return {
      formState,
      onFinishFailed,
      setForm,
      formRef,
      onFinished,
      plainOptions,
      roleOptions,
      isEdit,
    };
  },
});
</script>
  
  